import { _decorator, Component, Label, Node, Sprite, UIOpacity } from 'cc';
const { ccclass, property } = _decorator;
import { Game } from '../core/Game';
import { Databus } from '../core/Databus';
import { EventManager } from '../core/EventManager';
import { BilliardGame } from './BilliardGame';
import { ResourceManager } from '../framework/ResourceManager';
import { BallAttribute } from '../core/BallAttribute';

const databus = Databus.Instance();

@ccclass('PromptHint')
export class PromptHint extends Component {
  private switchMessage: Node = null;
  private hitBallTips: Node = null;
  private hitBallList: Node = null;
  private changeUserSeq = 1000;
  onLoad() {
    this.switchMessage = this.node.getChildByName('SwitchHintTips');
    this.hitBallTips = this.node.getChildByName('HitBallTips');
    this.hitBallList = this.node.getChildByName('HitBallList');

    EventManager.Instance().AddEventListener('ChangeUserHitBall', this, this.changeUserHitBall);
    EventManager.Instance().AddEventListener('StartGame', this, this.startGame);
  }

  update(deltaTime: number) {
    this.changeUserSeq += 1;
    if (this.changeUserSeq > 60) {
      this.switchMessage.active = false;
      this.hitBallTips.active = false;
    }
    // 展示当前用户击球列表
    if (Game.Instance().battleCate.isEndlessBattle()) {
      this.hitBallList.active = false;
      return;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      this.hitBallList.active = false;
      return;
    }
    this.hideHitBallList();
    if (BilliardGame.Instance().actionStage.canForceHit()) {
      this.hitBallList.active = true;
      var { hitBalls, protectBalls } = BilliardGame.Instance().getHitProtectBalls();
      
      var isSpecial = false;
      if (Game.Instance().gameType.isSnooker()) {
        if (Game.Instance().getCurrentPlayUserColor().isRedColorBall()) {
          this.updateHitBallList(['BallSphereRed'], isSpecial);
          return;
        } else if (Game.Instance().getCurrentPlayUserColor().isSpecialColor()) {
          // 倒序排列
          for (var i=protectBalls.length-1; i>=0; i--) hitBalls.push(protectBalls[i]);
          hitBalls = hitBalls.splice(0, 6);
          isSpecial = true;
        }
      } else if (Game.Instance().gameType.isEightBall()) {
        if (Game.Instance().getCurrentPlayUserColor().isUnknowColor()) {
          return;
        }
      } else if (Game.Instance().gameType.isNineBall()) {
        hitBalls = hitBalls.concat(protectBalls);
        hitBalls = hitBalls.splice(0, 6);
        isSpecial = true;
      }
      const ballList = hitBalls.map((ban: Node) => BallAttribute.getBallByName(ban.name));
      const ballNames = ballList.map((ba: BallAttribute) => ba.ballName());
      this.updateHitBallList(ballNames, isSpecial);
    }
  }

  
  public startGame() {
    if (Game.Instance().battleCate.isEndlessBattle()) {
      return;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      return;
    }
    this.changeUseHitBallActiveFalse();
    
    if (Game.Instance().isSelftHitBall()) {
      this.hitBallTips.getChildByName('SelfFirstHitBallSprite').active = true;
    } else {
      this.hitBallTips.getChildByName('OppoFirstHitBallSprite').active = true;
    }
  }
  public userContinueHitBall() {
    if (Game.Instance().battleCate.isEndlessBattle()) {
      this.hitBallTips.active = false;
      return;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      this.hitBallTips.active = false;
      return;
    }
    if (!Game.Instance().isSelftHitBall()) {
      this.hitBallTips.active = false;
      return;
    }
    this.changeUserSeq = 0;
    this.hitBallTips.active = true;
    this.hitBallTips.getChildByName('ContinueHitBallSprite').active = true;
    this.hitBallTips.getChildByName('ChangeHitBallSprite').active = false;
  }
  public changeUserHitBall() {
    if (Game.Instance().battleCate.isEndlessBattle()) {
      this.hitBallTips.active = false;
      return;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      this.hitBallTips.active = false;
      return;
    }
    this.changeUseHitBallActiveFalse();

    if (Game.Instance().isSelftHitBall()) {
      this.hitBallTips.getChildByName('MyTurnHitBallSprite').active = true;
    } else {
      this.hitBallTips.getChildByName('ChangeHitBallSprite').active = true;
    }
    this.switchMessage.getChildByName('HintTips').getComponent(Label).string = BilliardGame.Instance().illegaMessage;
    this.switchMessage.active = true;
  }
  private changeUseHitBallActiveFalse() {
    this.changeUserSeq = 0;
    this.hitBallTips.active = true;
    this.hitBallTips.getChildByName('OppoFirstHitBallSprite').active = false;
    this.hitBallTips.getChildByName('SelfFirstHitBallSprite').active = false;
    this.hitBallTips.getChildByName('ChangeHitBallSprite').active = false;
    this.hitBallTips.getChildByName('MyTurnHitBallSprite').active = false;
  }
  private hideHitBallList() {
    this.hitBallList.getChildByName('Ball1').getComponent(Sprite).spriteFrame = null;
    this.hitBallList.getChildByName('Ball2').getComponent(Sprite).spriteFrame = null;
    this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = null;
    this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = null;
    this.hitBallList.getChildByName('Ball5').getComponent(Sprite).spriteFrame = null;
    this.hitBallList.getChildByName('Ball6').getComponent(Sprite).spriteFrame = null;
    this.hitBallList.getChildByName('Ball7').getComponent(Sprite).spriteFrame = null;
  }
  private updateHitBallList(names: string[], isSpecial: boolean) {
    var opac = 255;
    if (isSpecial) {
      opac = 50;
      this.hitBallList.getChildByName('Ball1').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball2').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball3').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball4').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball5').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball6').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball7').getComponent(UIOpacity).opacity = opac;
    } else {
      this.hitBallList.getChildByName('Ball1').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball2').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball3').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball4').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball5').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball6').getComponent(UIOpacity).opacity = opac;
      this.hitBallList.getChildByName('Ball7').getComponent(UIOpacity).opacity = opac;
    }
    if (names.length === 1) {
      this.hitBallList.getChildByName('Ball4').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
    } else if (names.length === 2) {
      this.hitBallList.getChildByName('Ball3').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[1]);
    } else if (names.length === 3) {
      this.hitBallList.getChildByName('Ball3').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[1]);
      this.hitBallList.getChildByName('Ball5').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[2]);
    } else if (names.length === 4) {
      this.hitBallList.getChildByName('Ball2').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball2').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
      this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[1]);
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[2]);
      this.hitBallList.getChildByName('Ball5').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[3]);
    } else if (names.length === 5) {
      this.hitBallList.getChildByName('Ball2').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball2').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
      this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[1]);
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[2]);
      this.hitBallList.getChildByName('Ball5').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[3]);
      this.hitBallList.getChildByName('Ball6').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[4]);
    } else if (names.length === 6) {
      this.hitBallList.getChildByName('Ball1').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball1').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
      this.hitBallList.getChildByName('Ball2').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[1]);
      this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[2]);
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[3]);
      this.hitBallList.getChildByName('Ball5').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[4]);
      this.hitBallList.getChildByName('Ball6').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[5]);
    } else if (names.length === 7) {
      this.hitBallList.getChildByName('Ball1').getComponent(UIOpacity).opacity = 255;
      this.hitBallList.getChildByName('Ball1').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[0]);
      this.hitBallList.getChildByName('Ball2').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[1]);
      this.hitBallList.getChildByName('Ball3').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[2]);
      this.hitBallList.getChildByName('Ball4').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[3]);
      this.hitBallList.getChildByName('Ball5').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[4]);
      this.hitBallList.getChildByName('Ball6').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[5]);
      this.hitBallList.getChildByName('Ball7').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(names[6]);
    }
  }
}